package logic

import (
	"CampusCloudAid/dao/mysql"
	"CampusCloudAid/models"
	"CampusCloudAid/pkg/snowflake"
	"time"

	"go.uber.org/zap"
)

func GetAllTasks() (tasks []*models.Tasks, err error) {
	tasks, err = mysql.GetAllTasks()
	if err != nil {
		return
	}
	return tasks, nil
}

func GetTasksByConditional(p *models.ParamTasksByConditional) (tasks *[]models.Tasks, err error) {
	tasks, err = mysql.GetTasksByConditional(p)
	if err != nil {
		return
	}
	return tasks, nil
}

// GetTask 查询任务是否存在
func GetTask(id int64) (task *models.Tasks, err error) {
	task, err = mysql.GetTask(id)
	if err != nil {
		return nil, err
	}
	return task, nil
}

// UpdateReceiveInfo 修改任务申领人，申领时间和任务状态
func UpdateReceiveInfo(id int, uid int64) (err error) {
	if err = mysql.UpdateReceiveInfo(id, uid); err != nil {
		return
	}
	return
}

// SubmitVoucherTask 提交凭证，等待完成‘
func SubmitVoucherTask(id int, completionImage string) (err error) {
	if err = mysql.SubmitVoucherTask(id, completionImage); err != nil {
		return
	}
	return
}

func InputTask(id int64) (err error) {
	if err = mysql.InputTask(id); err != nil {
		return
	}
	return
}
func FinishTask(id int64) (err error) {
	if err = mysql.FinishTask(id); err != nil {
		return
	}
	return
}
func DownTask(id int64) (err error) {
	if err = mysql.DownTask(id); err != nil {
		return
	}
	return
}

// CreateTask -创建任务
func CreateTask(p *models.CreateTask) (err error) {
	//创建一个id
	Uid, err := snowflake.GetID()
	if err != nil {
		zap.L().Error("generation UID failed", zap.Error(err))
		return
	}

	p.Id = int64(Uid)
	p.TaskStatus = "发布"
	/*	Tasks := &models.CreateTask{
		Id:          int(Uid),
		PublisherId: p.PublisherId,
		TaskName:    p.TaskName,
		PaymentFee:  p.PaymentFee,
		TaskContent: p.TaskContent,
		TaskStatus:  p.TaskStatus,
		TaskType:    p.TaskType,
	}*/
	err = mysql.TaskPublishMoney(p.PublisherId, p.Id, p.PaymentFee) //对钱包表进行操作；
	if err != nil {
		return err
	}
	//钱足够通过才能存入数据库
	p.OnlineTime = time.DateTime
	if err = mysql.CreateTask(p); err != nil {
		return err
	}

	return
}

// ReceiveTasks 接收任务
/*func ReceiveTasks(tid int, receiveId int64) {
	task := &models.Tasks{
		ReceiverId: receiveId,
		TaskStatus: "已接收",
	}
	mysql.ReceiveTasks(tid, task)
}*/

// TimedShutdown 定时下架任务
func TimedShutdown(p *models.CreateTask) (err error) {

	//创建一个定时器
	time.Sleep(2000 * time.Hour)
	//查看订单状态，查看是否已经被接收
	task, err := mysql.GetTask(p.Id)
	if task.TaskStatus == "发布" {

		//当订单状态为发布时，修改订单状态为已下架
		task := &models.Tasks{
			TaskStatus: "已下架",
		}
		if err = mysql.ReceiveTasks(int(p.Id), task); err != nil {
			return err
		}
		// 下架后将任务中的钱返回给发起任务的用户
		err = mysql.TaskUnpublishMoney(p.PublisherId, p.Id, p.PaymentFee)
		if err != nil {
			return err
		}
		return
	}

	//修改订单的状态

	return
}

// GetReceivePhone 查询申领人手机号是否存在
func GetReceivePhone(id int) (phone string, err error) {
	phone, err = mysql.GetReceivePhone(id)
	if err != nil {
		return "", err
	}
	return phone, nil
}

// GetPublishPhone 查询申领人手机号是否存在
func GetPublishPhone(id int) (phone string, err error) {
	phone, err = mysql.GetPublishPhone(id)
	if err != nil {
		return "", err
	}
	return phone, nil
}

// GetTasksByStatus 根据状态查询任务
func GetTasksByStatus(status string) (tasks []*models.Tasks, err error) {
	tasks, err = mysql.GetTasksByStatus(status)
	if err != nil {
		return
	}
	return tasks, nil
}

// UpdateTaskImage 更新任务图片
func UpdateTaskImage(Id int64, image []byte) error {
	err := mysql.UpdateTaskImage(Id, image)
	if err != nil {
		return err
	}
	return nil
}
